define(
    ["PDUtilDir/dataService", "PDUtilDir/grid", "AppPathDir/order/orderForVisitorProductInfos/addVisitor", "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar", "PDUtilDir/inputSelect", "Date", "DateCN", "css!DateCss"],
    function (DataService, Grid, AddVisitorSupport, Util, Tool, Slidebar, inputSelect) {
        function agencyUpdateSupportInit($scope, $compile) {
            return new agencyUpdateSupport($scope, $compile);
        };

        function agencyUpdateSupport($scope, $compile) {
            this.$scope = $scope;
            this.$compile = $compile;
        };
        agencyUpdateSupport.fn = agencyUpdateSupport.prototype;

        agencyUpdateSupport.fn.init = function (data) {
            var _this = this;

            _this.entity = $.extend(data, {
                visitors: new Array()
            });

            _this.cache = {
                certificates: new Array(),
                certificatesObject: new Map()
            };
            $.ajax({
                url : getServer() + "/flea/hclv/visitor/selectByVisitorId.htmls",
                data: {
                    visitorId: _this.entity.visitorId
                },
                success : function(data) {
                    if (data.status != 200) {
                        return;
                    }
                    _this.entity.visitors.push(data.data);
                    _this.visitorInfoInit();
                }
            });

            $.ajax({
                url : getServer() + "/flea/hclv/getCertificates.htmls",
                success : function(data) {
                    if (data.status != 200) {
                        return;
                    }
                    _this.cache.certificates = data.data;
                    $.each(data.data, function(index, row) {
                        _this.cache.certificatesObject.set(row.dictCode, row.dictName);
                    });
                    _this.visitorInfoInit();
                }
            });

        };

        agencyUpdateSupport.fn.digest = function () {
            this.$scope.$digest();
        };

        agencyUpdateSupport.fn.run = function (id) {
            var _this = this;
            _this.init(id);

            this.slidebars = Slidebar({
                size: 'lg',
                url: getServer()
                + "/static/app/order/orderForVisitorProductInfos/views/update.html",
                $scope: this.$scope,
                $compile: this.$compile,
                afterLoad: function ($Panel) {
                    _this.initialization();
                }
            });
        };
        agencyUpdateSupport.fn.initialization = function () {
            this.baseInit();
        };
        agencyUpdateSupport.fn.baseInit = function () {
            var _this = this;
        };

        /* 添加游客 */
        agencyUpdateSupport.fn.addVisitorSupportFun = function (){
            if(this.addVisitorSupport)
                return this.addVisitorSupport;
            return this.addVisitorSupport = AddVisitorSupport(this.$scope, this.$compile)
        };

        // 刷新游客列表
        agencyUpdateSupport.fn.reloadVisitorsSupport = function(){
            var _this = this;
            return {
                run : function() {
                    var selecteds = _this.entity.visitors = new Array(),
                        datas = DataService.getObject('product.agency.search.visitors');

                    $.each(datas, function(index, row) {
                        selecteds.push($.extend(row, {
                            uuid: new Date().getTime(),
                            visitorId: row.id
                        }));
                    });
                    _this.gird.reload();
                }
            }
        };
        agencyUpdateSupport.fn.save = function() {
            var _this = this, visitorId = undefined;
            $.each(_this.entity.visitors, function(index, row) {
                visitorId = row.id;
            });
            $.ajax({
                url : getServer() + "/flea/hclv/productAgency/updateAgency.htmls",
                data : {
                    'model' : JSON.stringify({
                        id: _this.entity.id,
                        visitorId: visitorId,
                        timeTableId: _this.entity.timeTableId,
                        prouductId: _this.entity.prouductId,
                        agencyedDept: _this.entity.agencyedDept,
                        totalSale: _this.entity.totalSale,
                        totalCost: _this.entity.totalCost
                    })
                },
                success : function(data) {
                    if (data.status != 200) {
                        return;
                    }
                    _this.$scope.grid.refresh();
                    Util.alert(data.message);
                    _this.slidebars.close();
                }
            });
        };
        agencyUpdateSupport.fn.visitorInfoInit = function () {
            var _this = this;

            if(_this.entity.visitors.length > 0 && _this.cache.certificates.length > 0)
                _this.gird = Grid({
                    id: "agencyVisitorGirdDivId",
                    pageSize: 10,
                    index: true,
                    multi: false,
                    pagination: true,
                    cache: false,
                    layout : [ {
                        name : "中文名",
                        field : "zhName",
                        width: "60px",
                        sort : true
                    }, {
                        name : "英文名",
                        field : "enName",
                        width: "60px",
                        sort : true
                    }, {
                        name : "性别",
                        field : "sex",
                        width: "40px",
                        format : function(e) {
                            if (e.row.sex == "1" || e.row.sex == 1)
                                return '男';
                            else
                                return '女';
                        }
                    }, {
                        name : "年龄",
                        width: "40px",
                        field : "age"
                    }, {
                        name : "联系电话",
                        width: "80px",
                        field : "phone"
                    }, {
                        name : "证件类型",
                        field : "certificates",
                        width: "60px",
                        format:function(e){
                            return _this.cache.certificatesObject.get(e.row.certificates);
                        }
                    }, {
                        name : "证件号码",
                        field : "certificatesNumber"
                    } ],
                    toolbar: [
                        {
                            name: "替换",
                            icon: "fa fa-search",
                            callback: function (event) {
                                _this.addVisitorSupportFun().run(true);
                            }
                        }
                    ],
                    data: this.entity.visitors
                });
        };
        return agencyUpdateSupportInit;
    });